package edu.calpoly;
public class Stone {
	public static final int EMPTY = 0;
	public static final int WHITE = 1;
	public static final int BLACK = 2;
	
	private int state;
	private boolean inAtari;
	private boolean isLegal;
	private int initSides;
	private int freeSides;
	private int blacks; //Number of its neighbors that are black
	private int whites; //Number of its neighbors that are white 
	                    // These 2 ints above are only updated in getFreeSidesReverse since we don't care for neighbor colors for regular stones/groups.
	private int x,y;
	private Stone[] neighbors = new Stone[4];
	
	public void setNeighbors(Stone[] neigh){
		neighbors = neigh;
	}
	
	public int getX(){
		return x;
	}
	public int getY(){
		return y;
	}
	
	public Stone(int x, int y){
	   this.x = x;
	   this.y = y;
	   state = EMPTY;
	   inAtari = false;
	   isLegal = true;
	   initSides = 4;
	   freeSides = 4;
	}
	
	public String toString()
	{
		if(state == EMPTY) return ".";
		else if(state == WHITE) return "w";
		else return "b";
	}
	
	/**
	 * Checks whether or not a stone is of the state empty
	 * @return true if the stone is in empty state
	 */
	public boolean isEmpty() {
		return state == EMPTY;
	}
	
	/**
	 * Returns the state, Stone.EMPTY, Stone.BLACK, or Stone.WHITE
	 * @return the state of the stone
	 */
	public int getState() {
		return state;
	}
	
	/**
	 * Sets whether or not a stone is a legal position to play on
	 * @param isLegal true if the position is legal
	 */
	public void setLegal(boolean isLegal) {
		this.isLegal = isLegal;
	}
	
	/**
	 * Returns whether or not a stone is a legal position to play on.
	 * @return true if the position is legal
	 */
	public boolean isLegal() {
		return isLegal;
	}
	
	/**
	 * Sets whether or not a stone/position is in atari
	 * @param inAtari true if the stone/position is in atari
	 */
	public void setInAtari(boolean inAtari) {
		this.inAtari = inAtari;
	}
	
	/**
	 * Returns whether or not the stone/position is in atari
	 * @return true if the stone/position is in atari
	 */
	public boolean isInAtari() {
		return inAtari;
	}
	
	/**
	 * Sets the number of free sides for the stone/position
	 *
	 * @param The number of free sides the stone/position has.
	 */
	public void setInitSides(int sides){
	   freeSides = initSides = sides;
	}
	public void setFreeSides(int sides){
	   freeSides = sides;
	}
	
	/**
	 * @return The number of free sides for the stone/position.
	 */
	public int getFreeSides(){
		int freeSides = 4;
		for(int i = 0; i < 4; i++){
			if(neighbors[i] == null || !neighbors[i].isEmpty())
				freeSides--;
		}
		
	   return freeSides;
	}
	
	/**
	 * This is backwards. Empty spaces don't account for free sides, colored intersections do.
	 *
	 * @return The number of free sides for the stone/position.
	 */
   public int getFreeSidesReverse(){
      int freeSides = 4;
      blacks = 0;
      whites = 0;
      
      for(int i = 0; i < 4; i++){
         if(neighbors[i] == null || neighbors[i].isEmpty())
            freeSides--;
         else{
            if (neighbors[i].getState() == WHITE){
               whites++;
            }
            else{
               blacks++;
            }
         }
      }
      
      return freeSides;
   }
   
   // Two very racist sounding methods.
   public int getBlackNeighbors(){
      return blacks;
   }
   
   public int getWhiteNeighbors(){
      return whites;
   }
   
   public Stone getNeighbor(int index){
      return neighbors[index];
   }
	
	public void setColor(String color){
      if (0 == color.compareToIgnoreCase("w")) {
         state = WHITE;
      }
      else{
         state = BLACK;
      }
	}
	
	public void subSides(){
	   freeSides--;
	}
	public void addSides(){
	   freeSides++;
	}
	public void resetStone(){
	   //freeSides = initSides;
	   state = EMPTY;
	   isLegal = true;
	}
}
